package com.sx.manage.service.consumer;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.sx.common.util.MsgCodeUtil;
import com.sx.common.util.NumberUtil;
import com.sx.common.util.json.JackJsonUtil;
import com.sx.manage.biz.consumer.IMConsumerEditInfoBiz;
import com.sx.manage.biz.consumer.IMConsumerEditLogDetailBiz;
import com.sx.manage.biz.consumer.bank.IMConsumerBankMapBiz;
import com.sx.manage.biz.consumer.info.IMNetworkPersonConsumerBiz;
import com.sx.manage.biz.consumer.relation.IMConsumerDataRelationBiz;
import com.sx.manage.service.bank.IMBaseConsumerBankMapService;
import com.sx.manage.service.consumer.info.IMConsumerOperationInfoService;
import com.sx.manage.service.consumer.util.MConsumerWebServiceUtil;
import com.sx.manage.service.sys.IMBaseSystemConfigInfoService;
import com.sx.manage.service.sys.IMSystemConfigInfoService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.manage.util.ExcelUtils;
import com.sx.manage.util.PropertiesUtils;
import com.sx.manage.util.WebServiceUtils;
import com.sx.model.constants.MDataStatusType;
import com.sx.model.constants.MSystemConstants;
import com.sx.model.constants.type.bank.MUserInfoBankValidate;
import com.sx.model.constants.type.bank.MUsingStatus;
import com.sx.model.constants.type.consumer.MConsumerDataRelationType;
import com.sx.model.constants.type.consumer.MConsumerEditAuth;
import com.sx.model.constants.type.consumer.MInfoEditApplyStatus;
import com.sx.model.exception.MRollbackException;
import com.sx.model.manage.mybatis.form.bank.MConsumerBankMapForm;
import com.sx.model.manage.mybatis.form.consumer.MPersonalConsumerNetworkForm;
import com.sx.model.manage.mybatis.form.consumer.edit.MConsumerEditLogDetailForm;
import com.sx.model.manage.mybatis.form.sys.MSystemConfigInfoForm;
import com.sx.model.manage.mybatis.vo.bank.MBankEleValidateVO;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerBankMapVO;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerDataRelationVO;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerEditLogDetailVO;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerOperationInfoVO;
import com.sx.model.manage.mybatis.vo.consumer.MPersonalConsumerNetworkVO;
import com.sx.model.manage.mybatis.vo.consumer.MPersonalConsumerVO;
import com.sx.model.manage.mybatis.vo.consumer.login.MConsumerLoginVO;
import com.sx.model.manage.mybatis.vo.importerror.MImpExcelErrorVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultExcelVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
import com.sx.model.manage.mybatis.vo.sys.MSysConfigVO;
import com.sx.model.util.FormClassTransforUtil;
import com.sx.model.util.StringsUtil;

/**
 * 个人用户信息
 * 
 * @author lenovo
 *
 */
@Service("personConsumerInfoWebServiceImpl")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class MPersonConsumerInfoWebServiceImpl {
	// 日志
	private Logger log = LoggerFactory.getLogger(this.getClass());

	// 用户信息新接口
	@Resource(name = "mNetworkPersonConsumerBizImpl")
	public IMNetworkPersonConsumerBiz mNetworkPersonConsumerBizImpl;

	@Resource(name = "mConsumerEditInfoBizImpl")
	private IMConsumerEditInfoBiz mConsumerEditInfoBizImpl;
	// 系统参数配置
	@Resource(name = "systemConfigInfoServiceImpl")
	private IMSystemConfigInfoService systemConfigInfoServiceImpl;
	// 系统参数配置
	@Resource(name = "mBaseSystemConfigInfoServiceImpl")
	private IMBaseSystemConfigInfoService mBaseSystemConfigInfoServiceImpl;
	// 二期 用户各种状态查询
	@Resource(name = "consumerOperationInfoServiceImpl")
	private IMConsumerOperationInfoService consumerOperationInfoServiceImpl;
	// 用户银行关系
	@Resource(name = "mBaseConsumerBankMapServiceImpl")
	private IMBaseConsumerBankMapService mBaseConsumerBankMapServiceImpl;

	@Resource(name = "mConsumerDataRelationBizImpl")
	private IMConsumerDataRelationBiz mConsumerDataRelationBizImpl;

	@Resource(name = "mConsumerBankMapBizImpl")
	private IMConsumerBankMapBiz mConsumerBankMapBizImpl;

	@Resource
	private IMConsumerEditLogDetailBiz consumerEditLogDetailBizImpl;

	@Resource(name = "webServiceUtils")
	private WebServiceUtils webServiceUtils;

	// web端 consumer util
	@Resource(name = "consumerWebServiceUtil")
	private MConsumerWebServiceUtil consumerWebServiceUtil;

	public IMSystemConfigInfoService getSystemConfigInfoServiceImpl() {
		return systemConfigInfoServiceImpl;
	}

	public void setSystemConfigInfoServiceImpl(IMSystemConfigInfoService systemConfigInfoServiceImpl) {
		this.systemConfigInfoServiceImpl = systemConfigInfoServiceImpl;
	}

	public IMNetworkPersonConsumerBiz getmNetworkPersonConsumerBizImpl() {
		return mNetworkPersonConsumerBizImpl;
	}

	public void setmNetworkPersonConsumerBizImpl(IMNetworkPersonConsumerBiz mNetworkPersonConsumerBizImpl) {
		this.mNetworkPersonConsumerBizImpl = mNetworkPersonConsumerBizImpl;
	}

	public IMConsumerEditInfoBiz getmConsumerEditInfoBizImpl() {
		return mConsumerEditInfoBizImpl;
	}

	public void setmConsumerEditInfoBizImpl(IMConsumerEditInfoBiz mConsumerEditInfoBizImpl) {
		this.mConsumerEditInfoBizImpl = mConsumerEditInfoBizImpl;
	}

	public IMConsumerOperationInfoService getConsumerOperationInfoServiceImpl() {
		return consumerOperationInfoServiceImpl;
	}

	public void setConsumerOperationInfoServiceImpl(IMConsumerOperationInfoService consumerOperationInfoServiceImpl) {
		this.consumerOperationInfoServiceImpl = consumerOperationInfoServiceImpl;
	}

	public IMBaseConsumerBankMapService getmBaseConsumerBankMapServiceImpl() {
		return mBaseConsumerBankMapServiceImpl;
	}

	public void setmBaseConsumerBankMapServiceImpl(IMBaseConsumerBankMapService mBaseConsumerBankMapServiceImpl) {
		this.mBaseConsumerBankMapServiceImpl = mBaseConsumerBankMapServiceImpl;
	}

	public IMBaseSystemConfigInfoService getmBaseSystemConfigInfoServiceImpl() {
		return mBaseSystemConfigInfoServiceImpl;
	}

	public void setmBaseSystemConfigInfoServiceImpl(IMBaseSystemConfigInfoService mBaseSystemConfigInfoServiceImpl) {
		this.mBaseSystemConfigInfoServiceImpl = mBaseSystemConfigInfoServiceImpl;
	}

	public IMConsumerDataRelationBiz getmConsumerDataRelationBizImpl() {
		return mConsumerDataRelationBizImpl;
	}

	public void setmConsumerDataRelationBizImpl(IMConsumerDataRelationBiz mConsumerDataRelationBizImpl) {
		this.mConsumerDataRelationBizImpl = mConsumerDataRelationBizImpl;
	}

	/**
	 * 跳转到添加界面
	 */
	public ViewAndModel toConsumerAdd(ViewAndModel model) {
		log.debug("用户信息新增界面：");

		String consumer_code = mNetworkPersonConsumerBizImpl.unique_consumerCode(MSystemConstants.CONSUMER_PREFIX_PERSON);
		MPersonalConsumerVO consumerVO = new MPersonalConsumerVO();
		consumerVO.setConsumer_code(consumer_code);

		model.addObject("consumerVO", consumerVO);

		MSystemConfigInfoForm form = new MSystemConfigInfoForm();
		form.setConfig_type(MSystemConstants.OPEN_ACCOUNT_CHANNEL_TYPES);
		List<MSysConfigVO> type_list = mBaseSystemConfigInfoServiceImpl.querySystemConfigList(form).getData();
		model.addObject("type_list", type_list);

		return model;

	}

	/**
	 * 跳转到添加界面
	 */
	public ResultBooleanVO save(MPersonalConsumerNetworkForm form, MConsumerLoginVO loginVO) {
		log.debug(" MPersonConsumerInfoWebServiceImpl   方法 save  调用form={}",form);
		ResultBooleanVO result = null;
		
		form.setConsumer_name(ExcelUtils.isChinese(form.getConsumer_name()));
		form.setBank_consumer_name(ExcelUtils.isChinese(form.getBank_consumer_name()));
		try {
			if (form.getConsumer_id() == null) {
				result = mNetworkPersonConsumerBizImpl.add_consumer(form, loginVO);
			} else {
				result = this.update_consumer_manager(form, loginVO);
			}
		} catch (Exception e) {
			log.error("用户信息编辑异常error={}", e);
			result = new ResultBooleanVO(false, null, "新增用户失败");
		}
		return result;
	}

	/** 线上注册用户审核 **/
	public ResultBooleanVO network_register_examine(Long[] consumer_ids, MConsumerLoginVO loginConsumer) {
		this.log.debug("开始调用三要素验证，需要验证的用户consumer_ids：" + NumberUtil.arrayToStr(consumer_ids));
		ResultBooleanVO result = null;
		List<MPersonalConsumerNetworkVO> consumerList = this.mNetworkPersonConsumerBizImpl.get_consumer_byIds(consumer_ids);
		if (consumerList == null) {
			log.error("需要验证的用户，在数据库内不存在");
			return new ResultBooleanVO(false, "m.Consumer.not.exist", null);
		}
		// 三要素验证功能状态
		boolean bankThreeElementsStatus = false;

		MSystemConfigInfoForm form = new MSystemConfigInfoForm();
		form.setConfig_name(MSystemConstants.BANK_THREE_ELEMENTS_STATUS);
		ResultObjectVO<MSysConfigVO> objResult = this.mBaseSystemConfigInfoServiceImpl.queryByIdSystemConfig(form);
		// 获取查询的数据
		MSysConfigVO systemConfigVO = objResult.getObjVo();
		log.error("开始调用MCIP的三要素验证接口");
		String value = systemConfigVO.getConfig_value();
		// 根据配置开判断
		if (MSystemConstants.AUTO_TAKE_MONEY_YES.equalsIgnoreCase(value)) {
			bankThreeElementsStatus = true;
		}
		StringBuilder error = new StringBuilder();
		try {
			String str = null;
			// 华夏三要素验证
			MBankEleValidateVO bankValidateVO = null;
			for (MPersonalConsumerNetworkVO consumer : consumerList) {
				log.info("三要素验证consumer={}", consumer);
				if (!MSystemConstants.NATIONALITY_CHINA_.equals(consumer.getNationality()) || !MSystemConstants.CREDENTIALS_IDENTITYCARD.equals(consumer.getLaw_type())) {
					log.error(consumer.getConsumer_name() + ":不是中国大陆身份证用户，请使用‘特殊鉴权’");
					error.append(consumer.getConsumer_name() + ":不是中国大陆身份证用户，请使用‘特殊鉴权’");
					continue;
				}
				// 未验证，验证失败的用户 需要验证
				if (MDataStatusType.DATA_VALIDATE_FAILURE.equals(consumer.getData_status()) || MDataStatusType.DATA_VALIDATE_NOT_YET.equals(consumer.getData_status())) {
					log.error(consumer.getConsumer_name() + ":不需要验证 ");
					error.append(consumer.getConsumer_name() + ":不需要验证");
					continue;
				}
				MConsumerBankMapVO bankMapVO = mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(consumer.getId());
				this.log.error("三要素验证数据封装：" + JackJsonUtil.objToJson(consumer));
				bankValidateVO = new MBankEleValidateVO();
				bankValidateVO.setBankNo(bankMapVO.getCard_num());
				bankValidateVO.setIdNo(consumer.getLaw_no());
				bankValidateVO.setName(consumer.getConsumer_name());

				log.error("正式调用三要素验证:" + JackJsonUtil.objToJson(bankValidateVO));
				if (bankThreeElementsStatus) {
					str = mNetworkPersonConsumerBizImpl.bank_three_elements(bankValidateVO);
				} else {
					log.info("三要素验证状态未开启，默认成功{}", bankValidateVO);
					str = "{success:true}";
				}
				//
				log.debug("三要素验证结果返回：" + str);
				ResultBooleanVO validateResult = consumerWebServiceUtil.edit_consumerKey_validate(consumer.getId(), str);
				if (!validateResult.isSuccess()) {
					log.info("consumer_name:{}三要素验证失败error={}", consumer.getConsumer_name(), validateResult.getMsg());
					error.append(validateResult.getMsg());
				}
				ResultBooleanVO networkResult = null;

				log.debug("开始封装给界面返回的数据");
				JSONObject jsonObj = new JSONObject(str);
				if ("FALSE".equalsIgnoreCase(jsonObj.get("success").toString())) {
					log.error("用户三要素认证失败");
					error.append(consumer.getConsumer_name() + ":验证失败");
					networkResult = this.mNetworkPersonConsumerBizImpl.consumer_state_up_date_batch(consumer.getId(), MDataStatusType.DATA_VALIDATE_FAILURE);
				} else if ("NOT_AUTH".equalsIgnoreCase(jsonObj.get("success").toString())) {
					log.error("用户三要素本次未认证");
					error.append(consumer.getConsumer_name() + ":" + jsonObj.get("info").toString());
					networkResult = this.mNetworkPersonConsumerBizImpl.consumer_state_up_date_batch(consumer.getId(), MDataStatusType.DATA_VALIDATE_NOT_YET);
				} else if ((boolean) jsonObj.get("success")) {
					log.debug("认证成功");
					networkResult = this.mNetworkPersonConsumerBizImpl.consumer_state_up_date_batch(consumer.getId(), MDataStatusType.DATA_AVAILABLE);
				} else {
					log.info("三要素验证返回结果为空 str={}", str);
					networkResult = new ResultBooleanVO("m.parameter.is.null");
				}
				if (!networkResult.isSuccess()) {
					return networkResult;
				}
				if (error.length() > 0) {
					result = new ResultBooleanVO(false, "m.user.three.elements.validate.error", error.toString());
					log.debug("错误信息提示" + error.toString());
					return result;
				}
			} // end for
			result = new ResultBooleanVO();
		} catch (Exception e) {
			log.debug("线上注册用户审核 error={}", e);
			result = new ResultBooleanVO("m.user.three.elements.validate.error");
		}

		return result;
	}

	/** 用户信息导入 ***/
	public ResultListVO<MImpExcelErrorVO> consumer_excel_import(MultipartFile file, MConsumerLoginVO loginConsumer, String channel_type) {
		log.debug("MPersonConsumerInfoWebServiceImpl 方法 consumer_excel_import 执行");
		if (null == file) {
			log.error("上传上来的文件为空");
			return new ResultListVO<>(false, null, "文件错误");
		}
		String fileName = file.getOriginalFilename();
		String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
		if (!"xlsx".equals(prefix)) {
			log.error(" 文件格式不对 prefix=" + prefix + "(目前只支持2007及以上版本Excel!，只能验证xlsx，)");
			ResultListVO<MImpExcelErrorVO> result = new ResultListVO<>(false, null, " 文件格式不对 prefix=" + prefix + "(目前只支持2007及以上版本Excel!，只能验证xlsx，)");
			result.setData(null);
			return result;
		}
		ResultExcelVO results = null;
		try {
			List<Map<String, String>> listMap = PropertiesUtils.proToMaps(MSystemConstants.PERSONAL_CONSUMER_IMPORT_FILE_PATH);
			results = ExcelUtils.fileToExcel(file, listMap.get(0), listMap.get(1));
		} catch (Exception e) {
			log.error("解析excel异常error={}", e);
			results = new ResultExcelVO(false, null, "excel解析异常，导入数据失败！");
		}

		if (!results.isSuccess()) {
			log.error("解析数据有错误提示" + results.getMsg());
			ResultListVO<MImpExcelErrorVO> result = new ResultListVO<>(false, null, results.getMsg());
			result.setData(null);
			return result;
		}
		// 为空，就获取默认的 开户渠道
		if (StringUtils.isBlank(channel_type)) {
			channel_type = webServiceUtils.defaultChannelType();
			log.info("获取系统默认使用的渠道channel_type={}", channel_type);
		}
		log.debug("解析数据完成开始验证数据");
		ResultListVO<MImpExcelErrorVO> excelResult = this.mNetworkPersonConsumerBizImpl.consumer_excel_import(results.getListMap(), loginConsumer, channel_type);
		Map<String, Object> paraMap = excelResult.getParamMap();
		if (null != paraMap) {
			List<Long> needReliveCardList = (List<Long>) paraMap.get("needReliveCardList");
			if (null != needReliveCardList) {
				for (Long consumer_id : needReliveCardList) {
					MImpExcelErrorVO errorVO = consumerWebServiceUtil.excelImportRelieveBank(consumer_id,loginConsumer);
					if(errorVO.getError().length()>0) {
						log.info("有错误提示error={}",errorVO.getError());
						excelResult.getData().add(errorVO);
					}
				}
			}
		}
		excelResult.setParamMap(null);
		return excelResult;
	}

	/**
	 * 用户三要素信息验证(新的华夏接口)
	 * 
	 * @param consumer_id
	 * @return
	 */
	public ResultBooleanVO threeElements(Long[] consumer_ids, MConsumerLoginVO loginConsumer) {

		log.debug("开始调用三要素验证，需要验证的用户consumer_ids：" + NumberUtil.arrayToStr(consumer_ids));
		ResultBooleanVO result = null;
		if (null == consumer_ids || 0 == consumer_ids.length) {
			log.info("用户三要素验证失败，用户id为空consumer_ids");
			return new ResultBooleanVO("m.parameter.is.null");
		}

		// 三要素验证功能状态
		boolean bankThreeElementsStatus = false;

		MSystemConfigInfoForm form = new MSystemConfigInfoForm();
		form.setConfig_name(MSystemConstants.BANK_THREE_ELEMENTS_STATUS);
		ResultObjectVO objResult = this.mBaseSystemConfigInfoServiceImpl.queryByIdSystemConfig(form);
		// 获取查询的数据
		MSysConfigVO systemConfigVO = (MSysConfigVO) objResult.getObjVo();
		log.error("开始调用MCIP的三要素验证接口");
		String value = systemConfigVO.getConfig_value();
		// 根据配置开判断
		if (MSystemConstants.AUTO_TAKE_MONEY_YES.equalsIgnoreCase(value)) {
			bankThreeElementsStatus = true;
		}
		StringBuilder error = new StringBuilder();
		try {
			String str = null;
			// 华夏三要素验证
			MBankEleValidateVO bankValidateVO = null;
			for (Long consumer_id : consumer_ids) {
				MPersonalConsumerVO consumer = this.mNetworkPersonConsumerBizImpl.get_consumer_byId_for_view(consumer_id).getObjVo();
				log.info("三要素验证consumer={}", consumer);
				if (null == consumer) {
					log.info("用户不存在consumerId={}", consumer_id);
					return new ResultBooleanVO(false, "m.Consumer.not.exist", null);
				}
				if (!MSystemConstants.NATIONALITY_CHINA_.equals(consumer.getNationality())) {
					log.error(consumer.getConsumer_name() + ":不是中国大陆身份证用户，请使用其它鉴权方式");
					error.append(consumer.getConsumer_name() + ":不是中国大陆身份证用户，请使用其它鉴权方式");
					continue;
				}
				if (!MSystemConstants.CREDENTIALS_IDENTITYCARD.equals(consumer.getLaw_type())) {
					log.error(consumer.getConsumer_name() + ":证件类型不是身份证，请使用其它鉴权方式");
					error.append(consumer.getConsumer_name() + ":证件类型不是身份证，请使用其它鉴权方式");
					continue;
				}
				MConsumerBankMapVO bankMapVO = mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(consumer.getId());
				this.log.error("三要素验证数据封装：" + JackJsonUtil.objToJson(consumer));
				if(MUserInfoBankValidate.VALIDATE_SUCCESS.equals(bankMapVO.getKey_validate())) {
					log.error("{}:{}",consumer.getConsumer_name(),MsgCodeUtil.getMsg("sx.m.consumer.bank.validate.success.not.repeat"));
					error.append(consumer.getConsumer_name()).append(":").append(MsgCodeUtil.getMsg("sx.m.consumer.bank.validate.success.not.repeat"));
					continue;
				}
				bankValidateVO = new MBankEleValidateVO();

				bankValidateVO.setBank_no(bankMapVO.getCard_num());
				bankValidateVO.setId_no(consumer.getLaw_no());
				bankValidateVO.setName(consumer.getConsumer_name());

				log.error("正式调用三要素验证:" + JackJsonUtil.objToJson(bankValidateVO));
				if (bankThreeElementsStatus) {
					str = mNetworkPersonConsumerBizImpl.bank_three_elements(bankValidateVO);
				} else {
					log.info("三要素验证状态未开启，默认成功{}", bankValidateVO);
					str = "{success:true}";
				}
				log.debug("三要素验证结果返回：" + str);
				result = consumerWebServiceUtil.edit_consumerKey_validate(bankMapVO.getId(), str);
				if (!result.isSuccess()) {
					log.error("更新三要素失败error={}", result.getMsg());
					error.append(consumer.getConsumer_name()).append("更新三要素状态失败");
				}
				log.debug("开始封装给界面返回的数据");
				str = StringsUtil.validateJsonToStr(consumer.getConsumer_name(), str);
				if (StringUtils.isNotBlank(str))
					error.append(str);
			}
			if (error.length() > 0) {
				result = new ResultBooleanVO(false, "m.user.three.elements.validate.error", error.toString());
				log.debug("错误信息提示" + error.toString());
				return result;
			}
			result = new ResultBooleanVO();
		} catch (Exception e) {
			log.error("华夏三要素异常error={}", e);
			result = new ResultBooleanVO("m.user.three.elements.validate.error");
		}
		return result;
	}

	/**
	 * 跳转到添加界面
	 */
	@RequestMapping(value = "toConsumerEdit")
	public void toConsumerEdit(MPersonalConsumerVO consumer, ViewAndModel model) {
		log.debug("用户信息修改：" + JackJsonUtil.objToJson(consumer));
		MPersonalConsumerVO consumerVO = mNetworkPersonConsumerBizImpl.get_consumer_byId_for_view(consumer.getId()).getObjVo();
		// 查询编辑记录组装数据
		consumerVO = this.mConsumerEditInfoBizImpl.get_consumer_byId_for_edit(consumerVO).getObjVo();
		model.addObject("consumerVO", consumerVO);

		MSystemConfigInfoForm form = new MSystemConfigInfoForm();
		form.setConfig_type(MSystemConstants.OPEN_ACCOUNT_CHANNEL_TYPES);
		List<MSysConfigVO> type_list = mBaseSystemConfigInfoServiceImpl.querySystemConfigList(form).getData();
		model.addObject("type_list", type_list);

	}

	// 三要素多线程内部类
	class MBankThreeElementsThread implements Callable<String> {
		private Logger logger = LoggerFactory.getLogger(this.getClass());

		private IMNetworkPersonConsumerBiz mNetworkPersonConsumerBizImpl;
		// id 数组
		private Long[] consumer_ids;
		// 当前登录用户
		private MConsumerLoginVO loginConsumer;

		// 构造函数
		public MBankThreeElementsThread(IMNetworkPersonConsumerBiz mNetworkPersonConsumerBizImpl, Long[] consumer_ids, MConsumerLoginVO loginConsumer) {
			super();
			this.mNetworkPersonConsumerBizImpl = mNetworkPersonConsumerBizImpl;
			this.consumer_ids = consumer_ids;
			this.loginConsumer = loginConsumer;
		}

		public Logger getLogger() {
			return logger;
		}

		public void setLogger(Logger logger) {
			this.logger = logger;
		}

		public IMNetworkPersonConsumerBiz getmNetworkPersonConsumerBizImpl() {
			return mNetworkPersonConsumerBizImpl;
		}

		public void setmNetworkPersonConsumerBizImpl(IMNetworkPersonConsumerBiz mNetworkPersonConsumerBizImpl) {
			this.mNetworkPersonConsumerBizImpl = mNetworkPersonConsumerBizImpl;
		}

		public Long[] getConsumer_ids() {
			return consumer_ids;
		}

		public void setConsumer_ids(Long[] consumer_ids) {
			this.consumer_ids = consumer_ids;
		}

		public MConsumerLoginVO getLoginConsumer() {
			return loginConsumer;
		}

		public void setLoginConsumer(MConsumerLoginVO loginConsumer) {
			this.loginConsumer = loginConsumer;
		}

		public String call() throws Exception {

			log.error("开始调用三要素验证，需要验证的用户consumer_ids：" + NumberUtil.arrayToStr(consumer_ids));
			List<MPersonalConsumerNetworkVO> consumerList = this.mNetworkPersonConsumerBizImpl.get_consumer_byIds(consumer_ids);
			if (consumerList == null) {
				String errorStr = "查询数据为空不能进行三要素验证 consumer_ids:" + NumberUtil.arrayToStr(consumer_ids);
				log.error(errorStr);
				return errorStr;
			}
			StringBuilder error = new StringBuilder();
			try {
				String str = null;
				// 华夏三要素验证
				MBankEleValidateVO bankValidateVO = null;
				for (MPersonalConsumerNetworkVO consumer : consumerList) {
					if (!MSystemConstants.NATIONALITY_CHINA_.equals(consumer.getNationality()) || !MSystemConstants.CREDENTIALS_IDENTITYCARD.equals(consumer.getLaw_type())) {
						log.error(consumer.getConsumer_name() + ":不是中国大陆，身份证用户，请使用‘特殊鉴权’");
						error.append(consumer.getConsumer_name() + ":不是中国大陆，身份证用户，请使用‘特殊鉴权’");
						continue;
					}
					MConsumerBankMapVO bankMapVO = mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(consumer.getId());
					log.debug("三要素验证数据封装：" + JackJsonUtil.objToJson(consumer));
					bankValidateVO = new MBankEleValidateVO();
					bankValidateVO.setBankNo(bankMapVO.getCard_num());
					bankValidateVO.setIdNo(consumer.getLaw_no());
					bankValidateVO.setName(consumer.getConsumer_name());
					log.debug("正式调用三要素验证" + JackJsonUtil.objToJson(bankValidateVO));
					str = mNetworkPersonConsumerBizImpl.bank_three_elements(bankValidateVO);
					//
					log.debug("三要素验证结果返回：" + str);
					ResultBooleanVO result = consumerWebServiceUtil.edit_consumerKey_validate(bankMapVO.getId(), str);
					if (!result.isSuccess()) {
						log.error("更新三要素失败error={}", result.getMsg());
						error.append(consumer.getConsumer_name()).append("更新三要素状态失败");
					}
					log.debug("开始封装给界面返回的数据");
					str = StringsUtil.validateJsonToStr(consumer.getConsumer_name(), str);
					if (StringUtils.isNotBlank(str))
						error.append(str);
				}
				if (error.length() > 0) {
					log.debug("错误信息提示：" + error.toString());
					return error.toString();
				}
			} catch (Exception e) {
				log.error("华夏开户异常error={}", e);
			}
			return null;
		}
	}

	/** 选择公司下拉列表 **/
	public ResultListVO<MPersonalConsumerNetworkVO> ajaxSerch(String str) {
		String q = str.split("&")[0];
		MPersonalConsumerNetworkForm form = new MPersonalConsumerNetworkForm();
		if (null != q) {
			String name;
			try {
				name = new String(q.split("=")[1].getBytes("iso8859_1"));
				form.setConsumer_name(java.net.URLDecoder.decode(name, "utf-8"));
			} catch (UnsupportedEncodingException e) {
				log.error("解析字符串异常error={}", e);
			}
		}
		return mNetworkPersonConsumerBizImpl.get_consumer_list(form);
	}

	/** 更新个人用户信息明细 **/
	public ResultBooleanVO update_consumer_manager(MPersonalConsumerNetworkForm form, MConsumerLoginVO loginVO) {
		// /新增状态，修改不用审核
		if (MDataStatusType.DATA_NEW_ADD.equals(form.getData_status())) {
			log.info("后台修改用户明细 consumer_id={}", form.getId());
			return mNetworkPersonConsumerBizImpl.update_consumer_manager(form, loginVO);
		} else {
			// 修改申请字段
			log.info("不是新增状态用户信息修改form={}", form);
			return consumerEditLogDetailBizImpl.editConsumerInfo(form, MConsumerEditAuth.USER_CNETER_AUTH, loginVO);
		}

	}

	/**
	 * 跳转到用户银行信息添加界面
	 */
	public ViewAndModel toConsumerAddBank(ViewAndModel model, Long consumer_id) {
		log.debug("跳转到用户银行信息添加界面consumer_id={}", consumer_id);
		model.setViewName("consumer/personal/bank/addConsumerBank");
		// 获取用户基本信息
		MPersonalConsumerNetworkVO consumerVO = this.mNetworkPersonConsumerBizImpl.get_consumer_byId(consumer_id).getObjVo();
		model.addObject("consumerVO", consumerVO);

		// 获取默认的开户渠道
		MConsumerDataRelationVO relationVO = this.mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(consumer_id, MConsumerDataRelationType.CONSUMER_PERSON_CHANNEL_TYPE).getObjVo();
		model.addObject("channel_type", relationVO);
		// 获取渠道列表
		MSystemConfigInfoForm form = new MSystemConfigInfoForm();
		form.setConfig_type(MSystemConstants.OPEN_ACCOUNT_CHANNEL_TYPES);
		List<MSysConfigVO> type_list = mBaseSystemConfigInfoServiceImpl.querySystemConfigList(form).getData();
		model.addObject("type_list", type_list);

		// 获取用户所有银行卡信息
		if (null != relationVO) {
			List<MConsumerBankMapVO> bankList = this.mBaseConsumerBankMapServiceImpl.query_bank_list_by_consumer_id(consumer_id, relationVO.getRelation_string_num()).getData();
			model.addObject("bankList", bankList);
		}
		return model;
	}

	/** 更新个人用户信息明细 **/
	public ResultBooleanVO saveConsumerBank(MPersonalConsumerNetworkForm form, MConsumerLoginVO loginVO) {
		log.debug("新增用户银行信息 consumer_id={}", form.getConsumer_id());
		MConsumerBankMapForm bankMapForm = FormClassTransforUtil.personalConsumerNetworkFormToConsumerBankMapForm(form);
		return mNetworkPersonConsumerBizImpl.insertConsumerbankMap(bankMapForm);
	}

	/**
	 * 跳转到用户银行信息添加界面
	 */
	public ViewAndModel bindBankCardView(ViewAndModel model, Long consumer_id) {
		log.debug("跳转到用户银行信息添加界面consumer_id={}", consumer_id);
		model.setViewName("consumer/personal/bank/consumerBankList");

		// 获取默认的开户渠道
		MConsumerDataRelationVO relationVO = this.mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(consumer_id, MConsumerDataRelationType.CONSUMER_PERSON_CHANNEL_TYPE).getObjVo();
		model.addObject("channel_type", relationVO);
		// 获取扩展信息
		MConsumerOperationInfoVO operationVO = consumerOperationInfoServiceImpl.queryByConsumerId(consumer_id).getObjVo();

		// 获取用户所有银行卡信息
		if (null != relationVO) {
			List<MConsumerBankMapVO> bankList = this.mBaseConsumerBankMapServiceImpl.query_bank_list_by_consumer_id(consumer_id, relationVO.getRelation_string_num()).getData();
			//
			for (MConsumerBankMapVO bankMapVO : bankList) {
				// 有默认的卡，或者默认等待的卡就表示已经绑定了入籍卡
				if (MUsingStatus.DEFAULT_USED.equals(bankMapVO.getStatus()) || MUsingStatus.DEFAULT_USED_WAIT.equals(bankMapVO.getStatus())) {
					model.addObject("hasBind", true);
					continue;
				}
			}
		}
		model.addObject("operationVO", operationVO);
		model.addObject("consumer_id", consumer_id);
		return model;
	}

	/**
	 * 跳转到用户银行信息添加界面
	 */
	public ResultListVO<MConsumerBankMapVO> bindBankCardList(Long consumer_id) {
		log.debug("跳转到用户银行信息添加界面consumer_id={}", consumer_id);
		// 获取默认的开户渠道
		MConsumerDataRelationVO relationVO = this.mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(consumer_id, MConsumerDataRelationType.CONSUMER_PERSON_CHANNEL_TYPE).getObjVo();

		// 获取用户所有银行卡信息
		if (null != relationVO) {
			MConsumerBankMapForm bankForm = new MConsumerBankMapForm();
			bankForm.setConsumer_id(consumer_id);
			bankForm.setChannel_type(relationVO.getRelation_string_num());
			ResultListVO<MConsumerBankMapVO> result = this.mConsumerBankMapBizImpl.queryListByForm(bankForm);
			Long num = Long.valueOf(0);
			if (null != result.getData()) {
				num = Long.valueOf(result.getData().size());
			}
			result.setRecordsTotal(num);
			result.setRecordsFiltered(num);
			return result;

		} else {
			return new ResultListVO<>(new ArrayList<>(0), Long.valueOf(0), Long.valueOf(0));
		}
	}

	/** 更新默认银行卡使用设置 **/
	public ResultBooleanVO defaultCardNum(Long consumer_id, Long bank_map_id) {
		log.debug("更新默认银行卡使用设置 consumer_id={},bank_map_id={}", consumer_id, bank_map_id);
		if (null == consumer_id) {
			log.info("更新默认银行卡使用设置 参数为空consumer_id={}", consumer_id);
			return new ResultBooleanVO("m.parameter.is.null");
		}
		// 验证现在是否有修改银行卡的请求，
		MConsumerEditLogDetailForm form = new MConsumerEditLogDetailForm();
		form.setConsumer_id(consumer_id);
		form.setEdit_status(MInfoEditApplyStatus.EDIT_APPLY);
		List<MConsumerEditLogDetailVO> applyList = consumerEditLogDetailBizImpl.queryList(form).getData();
		form.setEdit_status(MInfoEditApplyStatus.EDIT_PASS);
		List<MConsumerEditLogDetailVO> passList = consumerEditLogDetailBizImpl.queryList(form).getData();
		form.setEdit_status(MInfoEditApplyStatus.EDIT_PASS_MANAGE);
		List<MConsumerEditLogDetailVO> passManageList = consumerEditLogDetailBizImpl.queryList(form).getData();
		if (!passManageList.isEmpty() || !passList.isEmpty() || !applyList.isEmpty()) {
			log.info("用户有银行信息修改申请，不能修改默认银行卡consumer_id={},bank_map_id={}", consumer_id, bank_map_id);
			return new ResultBooleanVO(false, null, "用户有银行信息修改申请，不能修改默认银行卡");
		}

		ResultBooleanVO result = null;
		MConsumerBankMapVO bankMapVO = mBaseConsumerBankMapServiceImpl.query_consumerBankMap_for_manage(consumer_id, null);
		if (null != bankMapVO) {
			// 将用户所有再用的，可用的银行卡设置为可用状态
			MConsumerBankMapForm updateCan = new MConsumerBankMapForm();
			updateCan.setChannel_status(MDataStatusType.DATA_AVAILABLE);
			updateCan.setConsumer_id(bankMapVO.getConsumer_id());
			updateCan.setStatus(MUsingStatus.CAN_USED);
			updateCan.setId(bankMapVO.getId());
			result = mConsumerBankMapBizImpl.updateBankMapStatus(updateCan);
			if (!result.isSuccess()) {
				log.info("更新默认银行卡使用设置失败erros={}", result.getMsg());
				return result;
			}
		}
		// 将用户所有再用的，可用的银行卡设置为可用状态
		MConsumerBankMapForm updateCan = new MConsumerBankMapForm();
		updateCan.setChannel_status(MDataStatusType.DATA_AVAILABLE);
		updateCan.setConsumer_id(consumer_id);
		updateCan.setStatus(MUsingStatus.DEFAULT_USED);
		updateCan.setId(bank_map_id);
		result = mConsumerBankMapBizImpl.updateBankMapStatus(updateCan);
		if (!result.isSuccess()) {
			log.info("更新默认银行卡使用设置失败erros={}", result.getMsg());
			throw new MRollbackException(result.getMsg());
		}
		// 修改用户选中的银行卡为 默认使用状态
		return result;
	}

	/** 银行卡信息验证 */
	public ResultBooleanVO artificial_key_validate(Long[] consumer_ids) {
		return consumerWebServiceUtil.artificial_key_validate(consumer_ids);
	}

	/** 银行卡信息验证 */
	public ResultBooleanVO artificial_key_validate(Long consumer_id, Long bank_map_id) {
		return consumerWebServiceUtil.artificial_key_validate(consumer_id, bank_map_id);
	}

	/** 直通鉴权 **/
	public ResultBooleanVO directAuthentication(Long[] consumer_ids) {
		return consumerWebServiceUtil.directAuthentication(consumer_ids);
	}

	/** 直通鉴权 **/
	public ResultBooleanVO directAuthentication(Long consumer_id, Long bank_map_id) {
		return consumerWebServiceUtil.directAuthentication(consumer_id, bank_map_id);
	}

	/**
	 * 用户三要素信息验证
	 * 
	 * @param consumer_id
	 * @return
	 */
	public ResultBooleanVO threeElementsByBankMapId(Long consumer_id, Long bank_map_id) {
		log.debug("开始调用三要素验证，需要验证的用户consumer_id={},bank_map_id={}", consumer_id, bank_map_id);
		ResultBooleanVO result = null;
		if (null == consumer_id || null == bank_map_id) {
			log.info("用户三要素验证失败，参数为空consumer_id={},bank_map_id={}", consumer_id, bank_map_id);
			return new ResultBooleanVO("m.parameter.is.null");
		}
		// 三要素验证功能状态
		boolean bankThreeElementsStatus = false;

		MSystemConfigInfoForm form = new MSystemConfigInfoForm();
		form.setConfig_name(MSystemConstants.BANK_THREE_ELEMENTS_STATUS);
		ResultObjectVO objResult = this.mBaseSystemConfigInfoServiceImpl.queryByIdSystemConfig(form);
		// 获取查询的数据
		MSysConfigVO systemConfigVO = (MSysConfigVO) objResult.getObjVo();
		log.error("开始调用MCIP的三要素验证接口");
		String value = systemConfigVO.getConfig_value();
		// 根据配置开判断
		if (MSystemConstants.AUTO_TAKE_MONEY_YES.equalsIgnoreCase(value)) {
			bankThreeElementsStatus = true;
		}
		StringBuilder error = new StringBuilder();
		try {
			String str = null;
			// 华夏三要素验证
			MBankEleValidateVO bankValidateVO = null;
			MPersonalConsumerNetworkVO consumer = this.mNetworkPersonConsumerBizImpl.get_consumer_byId(consumer_id).getObjVo();
			log.info("三要素验证consumer={}", consumer);
			if (null == consumer) {
				log.info("用户不存在consumerId={}", consumer_id);
				return new ResultBooleanVO(false, "sx.m.consumer.is.not.exits", null);
			}
			if (!MSystemConstants.NATIONALITY_CHINA_.equals(consumer.getNationality())) {
				error.append(consumer.getConsumer_name()).append(":不是中国大陆身份证用户，请使用其它鉴权方式");
				log.error(error.toString());
				return new ResultBooleanVO(false, null, error.toString());
			}
			if (!MSystemConstants.CREDENTIALS_IDENTITYCARD.equals(consumer.getLaw_type())) {
				error.append(consumer.getConsumer_name()).append(":证件类型不是身份证，请使用其它鉴权方式");
				log.error(error.toString());
				return new ResultBooleanVO(false, null, error.toString());
			}
			MConsumerBankMapVO bankMapVO = mBaseConsumerBankMapServiceImpl.query_consumer_bank_map_by_id(bank_map_id).getObjVo();
			this.log.error("三要素验证数据封装：" + JackJsonUtil.objToJson(consumer));
			bankValidateVO = new MBankEleValidateVO();

			bankValidateVO.setBank_no(bankMapVO.getCard_num());
			bankValidateVO.setId_no(consumer.getLaw_no());
			bankValidateVO.setName(consumer.getConsumer_name());

			log.error("正式调用三要素验证:" + JackJsonUtil.objToJson(bankValidateVO));
			//
			log.error("正式调用三要素验证:" + JackJsonUtil.objToJson(bankValidateVO));
			if (bankThreeElementsStatus) {
				str = mNetworkPersonConsumerBizImpl.bank_three_elements(bankValidateVO);
			} else {
				log.info("三要素验证状态未开启，默认成功{}", bankValidateVO);
				str = "{success:true}";
			}
			log.debug("三要素验证结果返回：" + str);
			result = consumerWebServiceUtil.edit_consumerKey_validate(bankMapVO.getId(), str);
			if (!result.isSuccess()) {
				log.error("更新三要素失败error={}", result.getMsg());
				error.append(consumer.getConsumer_name()).append("更新三要素状态失败");
			}
			log.debug("开始封装给界面返回的数据");
			str = StringsUtil.validateJsonToStr(consumer.getConsumer_name(), str);
			if (StringUtils.isNotBlank(str))
				error.append(str);
			if (error.length() > 0) {
				result = new ResultBooleanVO(false, "m.user.three.elements.validate.error", error.toString());
				log.debug("错误信息提示" + error.toString());
				return result;
			}
			result = new ResultBooleanVO();
		} catch (Exception e) {
			log.error("华夏三要素异常error={}", e);
			result = new ResultBooleanVO("m.user.three.elements.validate.error");
		}
		return result;
	}

}
